﻿using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
//
using Yuanna.Model;

namespace Yuanna.ViewModel
{
    public class YuannaViewModel : INotifyPropertyChanged
    {
        private YuannaDataContext _yuannaDb;
        private ObservableCollection<YuannaPath> _allPaths;

        public bool IsDataLoaded
        {
            get;
            private set;
        }

        public YuannaViewModel()
        {
            _yuannaDb = new YuannaDataContext();
        }
        
        // 所有路径
        public ObservableCollection<YuannaPath> AllPaths
        {
            get { return _allPaths; }
            set
            {
                _allPaths = value;
                NotifyPropertyChanged("AllPaths");
            }
        }

        // 持久化更改
        public void SaveChangesToDb()
        {
            _yuannaDb.SubmitChanges();
        }

        public YuannaPath GetYuannaPath(int argYuannaPathId)
        {
            return _yuannaDb.Paths.FirstOrDefault(p => p.Id == argYuannaPathId);
        }

        // 载入所有的路径数据
        public void LoadDataFromDb()
        {
            #region 每次载入前先删除所有已标记为删除的路径
            var paths = _yuannaDb.Paths.Where(pa => pa.IsDelete == true);
            foreach (YuannaPath path in paths)
            {
                // 先删除路径下的所有坐标点
                _yuannaDb.Points.DeleteAllOnSubmit(path.Points);

                // 再删除路径
                _yuannaDb.Paths.DeleteOnSubmit(path);
            }

            // 提交更改
            _yuannaDb.SubmitChanges();
            #endregion

            var pathsInDb = from YuannaPath p in _yuannaDb.Paths where p.IsDelete == false orderby p.Id descending select p;
            AllPaths = new ObservableCollection<YuannaPath>(pathsInDb);

            this.IsDataLoaded = true;
        }

        // 保存指定路径到数据表中
        public void AddPathToDb(YuannaPath newPath)
        {
            _yuannaDb.Paths.InsertOnSubmit(newPath);
            _yuannaDb.SubmitChanges();
        }

        /// <summary>
        /// 从viewmodel中删除指定路径
        /// 因为写入坐标点是异步的，所以刚创建完成的路径，无法直接删除，因为还会异步写入坐标点
        /// 所以删除路径的正确的做法是先标记为该路径已删除，然后再真正删除
        /// </summary>
        /// <param name="pathForDelete">要删除的路径</param>
        public void DeletePath(YuannaPath pathForDelete)
        {
            AllPaths.Remove(pathForDelete);

            // 标记此路径为已删除
            pathForDelete.IsDelete = true;
            _yuannaDb.SubmitChanges();

            NotifyPropertyChanged("AllPaths");
        }

        // 往路径中添加一个坐标点
        public void AddYuannaPoint(YuannaPoint newPoint)
        {
            _yuannaDb.Points.InsertOnSubmit(newPoint);
            _yuannaDb.SubmitChanges();
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify the app that a property has changed.
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
}
